﻿@* Remove this section if you are using bundling *@
@section Scripts {
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
}

@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Log in";

    // Initialize general page variables
    var email = "";
    var password = "";
    var rememberMe = false;

    // Setup validation
    Validation.RequireField("email", "You must specify an email address.");
    Validation.RequireField("password", "You must specify a password.");
    Validation.Add("password",
        Validator.StringLength(
            maxLength: Int32.MaxValue,
            minLength: 6,
            errorMessage: "Password must be at least 6 characters"));

    // If this is a POST request, validate and process data
    if (IsPost) {
        // is this a OAuth sign in request?
        string provider = Request.Form["provider"];
        if (!provider.IsEmpty()) {
            OAuthWebSecurity.RequestAuthentication(provider, "~/Account/RegisterService");
            return;
        } else if (Validation.IsValid()) {
            email = Request.Form["email"];
            password = Request.Form["password"];
            rememberMe = Request.Form["rememberMe"].AsBool();

            if (WebSecurity.UserExists(email) && WebSecurity.GetPasswordFailuresSinceLastSuccess(email) > 4 && WebSecurity.GetLastPasswordFailureDate(email).AddSeconds(60) > DateTime.UtcNow) {
                Response.Redirect("~/Account/AccountLockedOut");
                return;
            }

            // Attempt to log in to the Security object using provided creds
            if (WebSecurity.Login(email, password, rememberMe)) {
                var returnUrl = Request.QueryString["ReturnUrl"];
                if (returnUrl.IsEmpty()) {
                    Response.Redirect("~/");
                } else {
                    Context.RedirectLocal(returnUrl);
                }
            } else {
                ModelState.AddFormError("The user name or password provided is incorrect.");
            }
        }
    }
}

<hgroup class="title">
    <h1>@Page.Title.</h1>
    <h2>Enter your user name and password below.</h2>
</hgroup>

<section id="loginForm">
    <form method="post">
        @* If one or more validation errors exist, show an error *@
        @Html.ValidationSummary("Log in was unsuccessful. Please correct the errors and try again.", excludeFieldErrors: true, htmlAttributes: null)

        <fieldset>
            <legend>Log in to Your Account</legend>
            <ol>
                <li class="email">
                    <label for="email" @if (!ModelState.IsValidField("email"))
                                       {<text>class="error-label"</text>}>Email address</label>
                    <input type="text" id="email" name="email" value="@email" @Validation.For("email")/>
                    @* Write any user name validation errors to the page *@
                    @Html.ValidationMessage("email")
                </li>
                <li class="password">
                    <label for="password" @if(!ModelState.IsValidField("password")){<text>class="error-label"</text>}>Password</label>
                    <input type="password" id="password" name="password" @Validation.For("password")/>
                    @* Write any password validation errors to the page *@
                    @Html.ValidationMessage("password")
                </li>
                <li class="remember-me">
                    <input type="checkbox" id="rememberMe" name="rememberMe" value="true" @if (rememberMe) {<text>checked="checked"</text>} />
                    <label class="checkbox" for="rememberMe">Remember me?</label>
                </li>
            </ol>
            <input type="submit" value="Log in" />
        </fieldset>
    </form>
    <p>
        <a href="~/Account/Register">Don't have a Account?</a>
        <a href="~/Account/ForgotPassword">Did you forget your password?</a>
    </p>
</section>

<section class="social" id="socialLoginForm">
    <form method="post">
        <h2>Use another service to log in.</h2>
        @*<div class="message-info">
            <p>
                To let users of this site log in using their accounts from other sites such as Facebook, Twitter, 
                and Windows Live, you must update this site. For more information 
                read the <a href="http://go.microsoft.com/fwlink/?LinkID=226949">OAuth Guide</a>.
            </p>
        </div>*@
        <fieldset>
            <legend>Log in using another service</legend>            
            <input type="submit" name="provider" id="twitter" value="Twitter" title="Log in using your Twitter account." />            
        </fieldset>
    </form>
</section>
